package com.mengshu.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mengshu.mybatisplus.config.MybatisPlusMetaObjectHandler;
import com.mengshu.mybatisplus.properties.MybatisplusProperties;
import com.mengshu.token.api.LoginService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

@Configuration
@EnableConfigurationProperties(MybatisplusProperties.class)
public class MybatisplusAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MetaObjectHandler metaObjectHandler(MybatisplusProperties mybatisplusProperties, Optional<LoginService> loginApi) throws Throwable {
        mybatisplusProperties.check(loginApi);
        return new MybatisPlusMetaObjectHandler(loginApi.orElse(null));
    }
}
